package com.css.dzj.dzjap.modules.executorinfo.controller;

import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.util.BeanUtil;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.executorinfo.entity.ApExecutorInfoEntity;
import com.css.dzj.dzjap.modules.executorinfo.service.ExecutorInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 执法人员信息控制层
 */
@RestController
@RequestMapping(value = "/executorInfo")
public class ExecutorInfoController {
    //自动注入service层
    @Autowired
    private ExecutorInfoService executorInfoService;
    @Autowired
    BaseService baseService;
    /**
     * 分页查询执法人员信息
     */
    @GetMapping(value = "/listexecutorinfo")
    public R getAllExecutorInfo(@RequestParam(required = false,defaultValue = "1") Integer pageNo,
                                @RequestParam(required = false,defaultValue = "10") Integer pageSize){
        PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);
        baseService.logSuccess(3,"查询成功","2");
        return R.ok().put("data",executorInfoService.getAllExecutorInfo(pageRequest));
    }
    /**
     * 通过ID查询执法人员信息
     */
    @GetMapping(value = "/getexecutorinfo")
    public R getExecutorInfo(@RequestParam("executorid") String id){
        baseService.logSuccess(3,"查询成功","2");
        return R.ok().put("data",executorInfoService.getExecutorInfo(id));
    }

    /**
     * 保存执法人员信息
     * @param executorInfo
     * @return
     */
    @PostMapping(value = "/addexecutorinfo")
    public R addExecutorInfo(@RequestBody ApExecutorInfoEntity executorInfo){
        if (StringUtils.isEmpty(executorInfo.getExecutorName())|| StringUtils.isEmpty(executorInfo.getExecutorOrganization())||StringUtils.isEmpty(executorInfo.getExecutorIdCard())||
                StringUtils.isEmpty(executorInfo.getExecutorSex())|| StringUtils.isEmpty(executorInfo.getExecutorLevel())||StringUtils.isEmpty(executorInfo.getExecutorNationality())||
                StringUtils.isEmpty(executorInfo.getExecutorEducation())|| StringUtils.isEmpty(executorInfo.getExecutorCharacter())||StringUtils.isEmpty(executorInfo.getExecutorSuperviseStatus())||
                StringUtils.isEmpty(executorInfo.getExecutorCode())){
            return R.error("必填字段为空");
        }
        String executorInfoId = executorInfoService.addExecutorInfo(executorInfo);
        baseService.logSuccess(3,"保存成功","2");
        return R.ok().put("executorInfoId",executorInfoId);
    }

    /**
     * 根据ID逻辑删除执法人员信息
     */
    @PostMapping(value = "/delexecutorinfo/{executorId}")
    public R delExecutorInfo(@PathVariable  String executorId){
        if (executorInfoService.delExecutorInfo(executorId)){
            baseService.logSuccess(3,"删除成功","2");
            return R.ok().put("executorId",executorId);
        }else {
            baseService.logFailed(3,"删除失败","2");
            return R.error().put("executorId",executorId);
        }
    }

    /**
     * 修改执法人员信息
     * @param executorInfo
     * @return
     */
    @PostMapping(value = "/updexecutorinfo")
    public R updExecutorInfo(@RequestBody ApExecutorInfoEntity executorInfo){
        if (StringUtils.isEmpty(executorInfo.getExecutorName())|| StringUtils.isEmpty(executorInfo.getExecutorOrganization())||StringUtils.isEmpty(executorInfo.getExecutorIdCard())||
                StringUtils.isEmpty(executorInfo.getExecutorSex())|| StringUtils.isEmpty(executorInfo.getExecutorLevel())||StringUtils.isEmpty(executorInfo.getExecutorNationality())||
                StringUtils.isEmpty(executorInfo.getExecutorEducation())|| StringUtils.isEmpty(executorInfo.getExecutorCharacter())||StringUtils.isEmpty(executorInfo.getExecutorSuperviseStatus())||
                StringUtils.isEmpty(executorInfo.getExecutorCode())){
            return R.error("必填字段为空");
        }
        try{
            String executorId = executorInfo.getId();
            ApExecutorInfoEntity eInfo = executorInfoService.getExecutorInfo(executorId);
            BeanUtil.copyBeanNotNullToBean(executorInfo,eInfo);
            executorInfoService.updExecutorInfo(eInfo);
            baseService.logSuccess(3,"修改成功","2");
            return R.ok().put("executorId",executorId);
        }catch (Exception e){
            baseService.logFailed(3,"修改失败","2");
            return R.error("操作失败");
        }
    }

    /**
     * 根据专家等级分页查询执法人员信息
     * @param executorLevel
     * @param pageNo
     * @param pageSize
     * @return
     */
    @PostMapping(value = "/findexecutorbylevel")
    public R findExecutorByLevel(@RequestParam String executorLevel,
                                 @RequestParam(required = false,defaultValue = "1") Integer pageNo,
                                 @RequestParam(required = false,defaultValue = "10") Integer pageSize){
        if (StringUtils.isEmpty(executorLevel)){
            return R.error("请输入执法人员等级");
        }
        PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);
        baseService.logSuccess(3,"查询成功","2");
        return R.ok().put("data",executorInfoService.findExecutorByLevel(executorLevel,pageRequest));
    }

    /**
     * 多条件分页查询执法人员信息
     * @param executorName 姓名
     * @param executorCode 执法证号
     * @param executorOrganization 所属主体（受委托组织）的全称。
     * @param executorLevel 职级
     * @param executorCharacter 性质
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/getlistexecutorinfo")
    public R getListExecutorInfo(@RequestParam(required = false) String executorName,
                                 @RequestParam(required = false) String executorCode,
                                 @RequestParam(required = false) String executorOrganization,
                                 @RequestParam(required = false) String executorLevel,
                                 @RequestParam(required = false) String executorCharacter,
                                 @RequestParam(required = false,defaultValue = "1") Integer pageNo,
                                 @RequestParam(required = false,defaultValue = "10") Integer pageSize
                                 ){
        PageRequest pageRequest = PageRequest.of(pageNo-1,pageSize);
        baseService.logSuccess(3,"查询成功","2");
        return R.ok().put("data",executorInfoService.getListExecutorInfo(pageRequest,executorName,executorCode,executorOrganization,executorLevel,executorCharacter));
    }
    /**
     * 查询所有执法人员信息
     */
    @GetMapping(value = "/alllist")
    public List<ApExecutorInfoEntity> getAll(){
        List<ApExecutorInfoEntity> list = executorInfoService.selectAll();
        return list;
    }
    /**
     * 通过ID查询执法人员
     */
    @GetMapping(value = "/getOne")
    public ApExecutorInfoEntity getInfo(@RequestParam("executorid") String id){
        ApExecutorInfoEntity executorInfo = executorInfoService.getExecutorInfo(id);
        return executorInfo;
    }
    /**
     * 多条件查询执法人员信息、导出数据
     */
    @GetMapping(value = "/getSelectCon")
    public List<ApExecutorInfoEntity> getSelectCon( String executorName,String executorCharacter,String executorArea, String executorCategory){
        List<ApExecutorInfoEntity> executorInfo = executorInfoService.getSelectInfo(executorName,executorCharacter,executorArea,executorCategory);
        return executorInfo;
    }
    /**
     * 通过ids导出数据
     */
    @GetMapping(value = "/getIds")
    public List<ApExecutorInfoEntity> getSelectIds( String ids){
        if (com.alibaba.druid.util.StringUtils.isEmpty(ids)) {
            throw new RuntimeException("ids为空!");
        }
        List<String> idList = Arrays.asList(ids.split(","));
        List<ApExecutorInfoEntity> executorInfo = executorInfoService.getIds(idList);
        return executorInfo;
    }
}
